/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.simple.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.extra.copyProperties
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.simple.dao.SimpleListFieldDao
import com.gitee.minimalismstyle.fresh.simple.api.model.SimpleListField
import com.gitee.minimalismstyle.fresh.simple.api.model.SimpleListFieldAction
import com.gitee.minimalismstyle.fresh.simple.api.service.SimpleActionService
import com.gitee.minimalismstyle.fresh.simple.api.service.SimpleListFieldService
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 列表列定义业务实现
 * @author maoxiaodong
*/
@Primary
@Service
class SimpleListFieldServiceImpl: SimpleListFieldService, LongModelBaseServiceImpl<SimpleListFieldDao, SimpleListField>() {

    @Autowired
    private lateinit var simpleActionService: SimpleActionService

    override fun findByListId(listId: Long): R<List<SimpleListField>> {
        return R.success(dao.findByListId(listId))
    }

    @Transactional
    override fun saveSimpleListFieldActions(simpleListFieldActions: List<SimpleListFieldAction>): R<List<SimpleListFieldAction>> {
        val listId = simpleListFieldActions[0].listId
        if( listId != null){
            dao.deleteByListId(listId)
        }

        val simpleListFields = mutableListOf<SimpleListField>()
        simpleListFieldActions.forEach {
            if(it.action != null){
                simpleActionService.saveSimpleActionFields(it.action!!)
                it.actionId = it.action!!.id
            }
            simpleListFields.add(it.copyProperties(SimpleListField::class))
        }
        dao.insertAll(simpleListFields)

        return R.success(simpleListFields.map {simpleListField->
            val simpleListFieldAction = simpleListField.copyProperties(SimpleListFieldAction::class)
            val simpleListFieldHasAction = simpleListFieldActions.find { simpleListField.actionId == it.actionId }
            if(simpleListFieldHasAction != null){
                simpleListFieldAction.action = simpleListFieldHasAction.action
            }
            simpleListFieldAction
        })
    }

    override fun findSimpleListFieldActionByListId(listId: Long): R<List<SimpleListFieldAction>> {
        val simpleListFieldActions = dao.findByListId(listId).map {
            val simpleListFieldAction = SimpleListFieldAction()
            BeanUtils.copyProperties(it,simpleListFieldAction)
            val actionId = simpleListFieldAction.actionId
            if(actionId != null){
                simpleListFieldAction.action = simpleActionService.findSimpleActionFieldsById(actionId).get()
            }
            simpleListFieldAction
        }
        return R.success(simpleListFieldActions)
    }
}